package scuff.msg.json

import scuff.Surgeon
import scuff.msg.{MessageWriter, TypeChecker}
import scuff.msg.util.OutgoingMessage

/**
 * Writer of JSON messages of the format:
 * {{{
 * {
 *      "id": "<name>",
 *      "payload": <payload>
 * }
 * }}}
 * where `payload` is an optional field, with arbitrary data.
 */
trait JsonMessageWriter[-PL <: AnyRef, -S] extends MessageWriter[PL, S] with TypeChecker[String, PL] {

  /** Serialize full message into sink. */
  protected def serialize(message: OutgoingMessage[PL], sink: S)

  def write(payload: PL)(implicit sink: S) {
    serialize(new OutgoingMessage(typeID(payload), payload), sink)
  }

}
